package cn.dubito.sshl.idx.notice.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.search.highlight.Highlighter;
import org.springframework.stereotype.Repository;

import cn.dubito.sshl.common.PageModel;
import cn.dubito.sshl.dao.notice.dao.NoticeDao;
import cn.dubito.sshl.domain.Notice;
import cn.dubito.sshl.idx.impl.LuceneDaoImpl;
import cn.dubito.sshl.idx.notice.NoticeIndexDao;


/**
 * NoticeIndexDaoImpl
 */
@Repository("noticeIndexDao")
public class NoticeIndexDaoImpl extends LuceneDaoImpl implements NoticeIndexDao {

	/**
	 * 添加
	 * @param notice
	 */
	public void saveNotice(Notice notice){
		save(transfer(notice));
	}
	/**
	 * 修改
	 * @param notice
	 */
	public void updateNotice(Notice notice){
		update("id", String.valueOf(notice.getId()), transfer(notice));
	}
	
	/**
	 * 全文检索
	 * @param keyword
	 * @param pageModel
	 * @return
	 */
	public List<Notice> getNoticeByPage(String keyword, PageModel pageModel, NoticeDao noticeDao){
		try{
			String queryStr = "title:" + keyword + " content:" + keyword;
			List<Document> documents = this.findByPage(queryStr, pageModel);
			List<Notice> notices = new ArrayList<>();
			for (Document doc : documents){
				// 主键列
				int id = Integer.valueOf(doc.get("id"));
				Notice notice = noticeDao.get(Notice.class, id); // 持久化状态
				noticeDao.evict(notice); // 从一级缓存中踢出来
				// 获取高亮对象
				Highlighter highlighter = this.getHighlighter(queryStr, 80);
				// 获取高亮后的内容
				String title = highlighter.getBestFragment(analyzer, "title", notice.getTitle());
				title = StringUtils.isEmpty(title) ? notice.getTitle() : title;
				notice.setTitle(title);
				
				String content = highlighter.getBestFragment(analyzer, "content", notice.getContent());
				content = StringUtils.isEmpty(content) ? notice.getContent() : content;
				notice.setContent(content);
				
				notices.add(notice);
			}
			return notices;
		}catch(Exception ex){
			throw new RuntimeException(ex);
		}
	}
	/**
	 * 建全索引的方法
	 * @param noticeDao
	 */
	public void fullIndex(NoticeDao noticeDao){
		List<Notice> notices = noticeDao.find(Notice.class);
		deleteAll();
		for (Notice notice : notices){
			save(transfer(notice));
		}
	}
	
	/** 转换的方法 */
	private Document transfer(Notice notice) {
		Document doc = new Document();
		doc.add(new StringField("id", String.valueOf(notice.getId()), Store.YES));
		TextField title = new TextField("titile", notice.getTitle(), Store.NO);
		TextField content = new TextField("content", notice.getContent(), Store.NO);
		if (notice.getTitle().contains("李小华") || notice.getContent().contains("李小华")){
			title.setBoost(100f);
			content.setBoost(100f);
		}
		doc.add(title);
		doc.add(content);
		return doc;
	}

	@Override
	public String getDirectoryPath() {
		return IDX_BASE_DIR + File.separator + Notice.class.getSimpleName();
	}

}
